define(['jquery', 'bootstrap'], function($, undefined) {
    var FormDesign = {
        bindevent:function(form){
            FormDesign.faselect(form);
        },
        faselect: function(form) {
            //绑定fachoose选择附件事件
            if ($(".fachoose", form).size() > 0) {
                $(".fachoose", form).off('click').on('click', function() {
                    var that = this;
                    var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
                    var mimetype = $(this).data("mimetype") ? $(this).data("mimetype") : '';
                    var admin_id = $(this).data("admin-id") ? $(this).data("admin-id") : '';
                    var user_id = $(this).data("user-id") ? $(this).data("user-id") : '';
                    parent.Fast.api.open("formdesign/attachment/select?element_id=" + $(this).attr("id") + "&multiple=" + multiple + "&mimetype=" + mimetype + "&admin_id=" + admin_id + "&user_id=" + user_id, __('Choose'), {
                        callback: function(data) {
                            var button = $("#" + $(that).attr("id"));
                            var maxcount = $(button).data("maxcount");
                            var input_id = $(button).data("input-id") ? $(button).data("input-id") : "";
                            maxcount = typeof maxcount !== "undefined" ? maxcount : 0;
                            if (input_id && data.multiple) {
                                var urlArr = [];
                                var inputObj = $("#" + input_id);
                                var value = $.trim(inputObj.val());
                                if (value !== "") {
                                    urlArr.push(inputObj.val());
                                }
                                urlArr.push(data.url)
                                var result = urlArr.join(",");
                                if (maxcount > 0) {
                                    var nums = value === '' ? 0 : value.split(/\,/).length;
                                    var files = data.url !== "" ? data.url.split(/\,/) : [];
                                    var remains = maxcount - nums;
                                    if (files.length > remains) {
                                        Toastr.error(__('You can choose up to %d file%s', remains));
                                        return false;
                                    }
                                }
                                inputObj.val(result).trigger("change").trigger("validate");
                            } else {
                                $("#" + input_id).val(data.url).trigger("change").trigger("validate");
                            }
                        }
                    });
                    return false;
                });
            }
        }
    }
    return FormDesign;
});